package com.mashibing.class10;

public class Code01_FindFirstIntersectNode {
    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        // 求 head1 和 head2 的入环节点.
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        if (loop1 == null && loop2 == null) { // 两个链表(head1 和 head2) 都是 无环链表.
            return noLoop(head1, head2); // 两个无环链表的相交
        }
        if (loop1 != null && loop2 != null) { // 两个链表(head1 和 head2) 都是 有环链表.
            return bothLoop(head1, loop1, head2, loop2);// 两个有环链表的相交
        }
        // 两个链表(head1 和 head2) 一个是有环链表 一个是无环链表, 直接返回null.
        return null;
    }

    // 找到链表第一个入环节点，如果无环，返回null : 如果链表有环,返回第一个入环节点.
    public static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // slow是慢指针  fast是快指针
        Node slow = head.next;
        Node fast = head.next.next;
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) { // 快指针如果提前结束, 该链表无环.
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        // slow fast  相遇
        fast = head; // n2 -> walk again from head
        // 慢指针和快指针每次都只走一步.
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        // 快指针和慢指针再次相遇的时候, 返回慢指针. 该慢指针保存的是该节点的入环节点.
        return slow;
    }

    // 如果两个链表都无环，返回第一个相交节点，如果不想交，返回null
    public static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1.next != null) {
            n++;
            cur1 = cur1.next;
        }
        // 此时n的值是 cur1 链表的节点数.
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        // 此时n的值是 (cur1链表的节点数 - cur2链表的节点数)
        if (cur1 != cur2) {
            return null;
        }
        // n  :  链表1长度减去链表2长度的值
        cur1 = n > 0 ? head1 : head2; // 谁长，谁的头变成cur1
        cur2 = cur1 == head1 ? head2 : head1; // 谁短，谁的头变成cur2
        // 蒋长短链表重定向到 cur1 和 cur2 中.
        n = Math.abs(n);
        // cur1的链表长度 大于 cur2 的链表长度, 让cur1 先迈(cur1长度-cur2长度)个节点.
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        // 长链表(cur1) 和 短链表(cur2)一起走
        while (cur1 != cur2) { // cur1 和 cur2 两个链表一旦相遇的时候, 就从while循环中跳出来.
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1; // 返回相遇后的节点.
    }

    // 两个有环链表，返回第一个相交节点，如果不想交返回null
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        if (loop1 == loop2) { //和noLoop代码一样.
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }
            // 长短链表重定位.
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            // 长链表先走差值步.
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            // 长链表和短链表一起走.
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else { // 链表1 和 链表2 不一样.
            cur1 = loop1.next;// loop1 往下走.
            while (cur1 != loop1) {
                if (cur1 == loop2) { // loop1 和 loop2 相遇了.
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null; // loop1 和 loop2 一直没有相遇, 返回null.
        }
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }
}
